package com.springleaf.arena.display
{
	import com.springleaf.arena.state.CellState;
	import com.springleaf.common.display.UISprite;

	/**
	 * This is the base class for the Grid's cells. If you aren't a (Flash) developer
	 * you should not use this class but rather use the Engine class located a few
	 * layers above. (I don't know how many, you can count them yourself)
	 * 
	 * Contains basic data about a cell's state, position, animations, more or 
	 * less everything XD.
	 */
	public class UICell extends UISprite
	{
		private var currentAnimation:Array;
		private var currentFrame:int = -1;
		private var animationFunction:Function;
		
		private var _currentState:int;
		private var _idleState:Array;
		private var _xPosition:int;
		private var _yPosition:int;
		
		/**
		 * After creating a new cell, don't forget to set the sheet and the defaultSize.
		 * If you have no idea what I'm talking about, don't use this class, rather use the
		 * engine which will take care of all that for you.
		 */
		public function UICell(idleState:Array, currentState:int = CellState.IDLE_STATE):void {
			this._idleState = idleState;
			this.currentState = currentState;
		}
		
		/**
		 * Type all the animation states in the switch case statement so it can be loaded
		 */
		private function loadAnimation():void {
			switch (_currentState) {
				case CellState.IDLE_STATE :
					currentAnimation = idleState;
					break;
				default :
					currentAnimation = (currentAnimation ? currentAnimation : idleState);
					break;
			}
			loadAnimationFunction();
		}
		
		/**
		 * You must call the draw method before you will be able to use this method.
		 * If you don't know what I'm doing here please don't touch it or use it. Use the 
		 * engine instead, it does most of the things for you.
		 */
		public function updateAnimation():void {
			animationFunction();
		}
		
		/**
		 * You can use this method to draw the first default idle state.
		 * Should be used when you want to draw a default state (the first
		 * idle frame) and you know you won't start an animation.
		 */
		public function renderDefaultState():void {
			if (_idleState) {
				render(_idleState[0]);
			}
		}
		
		private function loadAnimationFunction():void {
			if (currentAnimation.length > 1) {
				animationFunction = drawNextFrame;
			} else {
				animationFunction = doNothing;
			}
		}
		
		private function doNothing():void {}
		
		private function drawNextFrame():void {
			if (++currentFrame < currentAnimation.length) {
				render(currentAnimation[currentFrame]);
			} else {
				currentFrame = 0;
				render(currentAnimation[currentFrame]);
			}
		}
		
		public function get idleState():Array
		{
			return _idleState;
		}
		
		public function set idleState(value:Array):void
		{
			_idleState = value;
		}
		
		public function get currentState():int
		{
			return _currentState;
		}
		
		/**
		 * Not only it setts the currentStae, but it updates the current animation as well.
		 */
		public function set currentState(value:int):void
		{
			_currentState = value;
			loadAnimation();
		}

		public function get xPosition():int
		{
			return _xPosition;
		}

		public function set xPosition(value:int):void
		{
			_xPosition = value;
		}

		public function get yPosition():int
		{
			return _yPosition;
		}

		public function set yPosition(value:int):void
		{
			_yPosition = value;
		}


	}
}